﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoolCode;
using CoolCode.Linq;
using CoolCode.Web;
using CoolCode.Web.Mvc;
using XNoteBook.Entities;
using XNoteBook.Services;
using XNoteBook.Models;

namespace XNoteBook.UI.Controllers
{
	public class OuterLinkController : CoolCode.Web.Mvc.ControllerBase
	{
		#region Services

		private ICommonService _CommonService;
		public ICommonService CommonService
		{
			get
			{
				if (_CommonService == null)
				{
					_CommonService = Digger.Dig<ICommonService>();
				}
				return _CommonService;
			}
		}

		private IPostService _PostService;
		public IPostService PostService
		{
			get
			{
				if (_PostService == null)
				{
					_PostService = Digger.Dig<IPostService>();
				}
				return _PostService;
			}
		}
		#endregion

		//
		// GET: /OuterLink/

		public ActionResult Index()
		{
			return View();
		}

		//
		// GET: /OuterLink/Create

		public ActionResult Create()
		{
			var model = new PostOuterInfoModel();
			model.CategoryID = this.ValueOf<string>("CategoryID");
			ViewData["StatusSource"] = this.CommonService.ListValueText("Status");
			return View("Edit", model);
		}

		public ActionResult Edit(string id)
		{
			var model = this.PostService.GetPostOuterInfoModel(id);

			if (model == null)
			{
				return this.RedirectToError("找不到相关信息！");
			}

			ViewData["StatusSource"] = this.CommonService.ListValueText("Status");
			return View(model);
		}


		[HttpGet]
		public ActionResult Import(string type, PostOuterInfoModel model)
		{
			try
			{
				this.PostService.Import(model, UserID);
				return this.Success();
			}
			catch (Exception ex)
			{
				return this.Fail(ex.Message);
			}
		}

		public ActionResult OuterInfoList(string q)
		{
			var qBuilder = QueryBuilder.Create<PostOuterInfo>();
			if (!string.IsNullOrEmpty(q))
			{
				qBuilder.Like(c => c.Description, q);
			}

			var model = this.PostService.QueryPostOuterInfo(qBuilder);
			return PartialView(model);
		}

		//
		// GET: /OuterLink/Delete/5

		public ActionResult Delete(string id)
		{
			try
			{
				this.PostService.DeletePostOuterInfo(id);
				return this.Success();
			}
			catch (Exception ex)
			{
				return this.Fail(ex.Message);
			}
		}

	}
}
